/**
 * 文章相关API接口
 */
import request from '../request.js'
import config from '../config.js'

// API接口基础路径
const API_PREFIX = config.apiBaseUrl + '/api/articles'

/**
 * 获取文章列表
 * @param {Object} params 查询参数 {category, page, pageSize, keyword}
 * @returns {Promise} Promise对象
 */
export const getArticleList = (params = {}) => {
  return request.get(`${API_PREFIX}/list`, params)
}

/**
 * 获取文章详情
 * @param {Number|String} id 文章ID
 * @returns {Promise} Promise对象
 */
export const getArticleDetail = (id) => {
  return request.get(`${API_PREFIX}/detail/${id}`)
}

/**
 * 获取文章分类列表
 * @returns {Promise} Promise对象
 */
export const getArticleCategories = () => {
  return request.get(`${API_PREFIX}/categories`)
}

/**
 * 获取热门文章列表
 * @param {Number} limit 返回数量限制
 * @returns {Promise} Promise对象
 */
export const getHotArticles = (limit = 5) => {
  return request.get(`${API_PREFIX}/hot`, { limit })
}

/**
 * 获取推荐文章列表
 * @param {Number} limit 返回数量限制
 * @returns {Promise} Promise对象
 */
export const getRecommendedArticles = (limit = 5) => {
  return request.get(`${API_PREFIX}/recommended`, { limit })
}

/**
 * 点赞文章
 * @param {Number|String} id 文章ID
 * @returns {Promise} Promise对象
 */
export const likeArticle = (id) => {
  return request.post(`${API_PREFIX}/like/${id}`)
}

/**
 * 收藏/取消收藏文章
 * @param {Number|String} id 文章ID
 * @returns {Promise} Promise对象
 */
export const favoriteArticle = (id) => {
  return request.post(`${API_PREFIX}/favorite/${id}`)
}

/**
 * 检查文章是否已收藏
 * @param {Number|String} id 文章ID
 * @returns {Promise} Promise对象
 */
export const checkArticleFavorite = (id) => {
  return request.get(`${API_PREFIX}/favorite/check/${id}`)
}

/**
 * 获取用户收藏的文章列表
 * @param {Object} params 查询参数 {page, pageSize}
 * @returns {Promise} Promise对象
 */
export const getFavoriteArticles = (params = {}) => {
  return request.get(`${API_PREFIX}/favorite/list`, params)
}

/**
 * 按分类获取文章列表
 * @param {String} category 分类编码
 * @param {Object} params 查询参数 {page, pageSize}
 * @returns {Promise} Promise对象
 */
export const getArticlesByCategory = (category, params = {}) => {
  return request.get(`${API_PREFIX}/category/${category}`, params)
}

/**
 * 搜索文章
 * @param {String} keyword 搜索关键词
 * @param {Object} params 查询参数 {page, pageSize}
 * @returns {Promise} Promise对象
 */
export const searchArticles = (keyword, params = {}) => {
  return request.get(`${API_PREFIX}/search`, { keyword, ...params })
}

/**
 * 获取所有标签
 * @returns {Promise} Promise对象
 */
export const getAllTags = () => {
  return request.get(`${API_PREFIX}/tags`)
}

export default {
  getArticleList,
  getArticleDetail,
  getArticleCategories,
  getHotArticles,
  getRecommendedArticles,
  likeArticle,
  favoriteArticle,
  checkArticleFavorite,
  getFavoriteArticles,
  getArticlesByCategory,
  searchArticles,
  getAllTags
} 